package com.laikan.reader.core.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ScrollView;

/**
 *
 */
public class MyScrollView extends ScrollView {

    private OnScrollListener onScrollListener;

    private MOverScrollBy mOverScrollBy;

    private MOnTouchEvent mOnTouchEvent;
    /** 处理往下拉时,在魅蓝note3上会自动一直调用overScrollBy()方法bug */
    private boolean isMove = false;

    public MyScrollView(Context context) {
        this(context, null);
    }

    public MyScrollView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    /**
     * 设置滚动接口
     *
     * @param onScrollListener
     */
    public void setOnScrollListener(OnScrollListener onScrollListener) {
        this.onScrollListener = onScrollListener;
    }

    /**
     * 设置下拉时的操作
     * @param mOverScrollBy
     */
    public void setmOverScrollBy(MOverScrollBy mOverScrollBy) {
        this.mOverScrollBy = mOverScrollBy;
    }

    public void setmOnTouchEvent(MOnTouchEvent mOnTouchEvent) {
        this.mOnTouchEvent = mOnTouchEvent;
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (onScrollListener != null) {
            onScrollListener.onScroll(this, l, t, oldl, oldt);
        }
    }

    @Override
    protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
        if (mOverScrollBy != null && isMove) {
            mOverScrollBy.mOverScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent);
        }
        isMove = false;

        return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (mOnTouchEvent != null) {
            mOnTouchEvent.mOnTouchEvent(ev);
        }
        return super.onTouchEvent(ev);
    }

    /**
     * 滚动的回调接口
     *
     * @author xiaanming
     */
    public interface OnScrollListener {

        /**
         * 回调方法， 返回MyScrollView滑动的Y方向距离
         *
         * @param myScrollView
         * @param l
         * @param t
         * @param oldl
         * @param scrollY 、
         */
        void onScroll(MyScrollView myScrollView, int l, int t, int oldl, int scrollY);
    }

    public interface MOnTouchEvent {

        void mOnTouchEvent(MotionEvent ev);
    }

    public interface MOverScrollBy {

        void mOverScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent);
    }

    public boolean isMove() {
        return isMove;
    }

    public void setMove(boolean move) {
        isMove = move;
    }
}
